package com.yueyang.sojourn.server.service.member;

import com.alibaba.fastjson.JSON;
import com.yueyang.sojourn.server.common.baymax.web.result.JAssert;
import com.yueyang.sojourn.server.model.po.User;
import com.yueyang.uc.model.UcUserV1;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 从uc同步的userId
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User findByUserId(Long userId) {
        return this.userMapper.selectByPrimaryKey(userId);
    }

    public void update(User user) {
        this.userMapper.updateByPrimaryKey(user);
    }

    /**
     * 批量insert
     * @param users
     */
    public void batchInsert(List<User> users) {
        if (CollectionUtils.isNotEmpty(users)) {
            userMapper.batchInsert(users);
        }
    }

    /**
     * 根据最新的ucUser 更新旅居的user
     * @param ucUserV1
     */
    public void updateByUcUser(UcUserV1 ucUserV1) {
        JAssert.pass(ucUserV1 != null && ucUserV1.getId() != null, "更新user异常，ucUserV1:" + JSON.toJSONString(ucUserV1));

        User user = this.findByUserId(ucUserV1.getId());
        if (user != null) {
            user.setMtime(new Date());
            user.setNickName(ucUserV1.getNickName());
            user.setPhone(ucUserV1.getPhone());
            user.setSex(ucUserV1.getSex());
            this.update(user);
        }
    }

    public User findNewest() {
        return this.userMapper.selectNewest();
    }
}
